import requests
import json

Authorization = "f1125690-49c6-4f7f-8a7c-f98ceb387c13"

url = "https://sqadmin.mot.gov.cn/pczz/api/v2/jtable/actions/region/query"
headers = {
    "Accept": "application/json, text/plain, */*",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Authorization": Authorization,
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Host": "sqadmin.mot.gov.cn",
    "Origin": "https://sqadmin.mot.gov.cn",
    "Pragma": "no-cache",
    "Referer": "https://sqadmin.mot.gov.cn/pczz/",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
    "X-Requested-With": "XMLHttpRequest",
    "language": "zh-CN",
    "sec-ch-ua": '"Chromium";v="124", "Microsoft Edge";v="124", "Not-A.Brand";v="99"',
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": '"Windows"'
}

payload = {
    "context": {
        "taskKey": "84699da2-66c0-4fe6-8209-05dbf572685c",
        "formKey": "08608346-8170-456f-b7c1-725ce075281c",
        "formSchemeKey": "a730ef68-c5de-4d67-a0a9-926e5c346a6d",
        "formulaSchemeKey": "91b04466-5df5-4baf-8903-0fc9a0cfe820",
        "dimensionSet": {
            "DATATIME": {
                "name": "DATATIME",
                "value": "2025R0006",
                "type": 6
            },
            "MD_ORG": {
                "name": "MD_ORG",
                "value": "330702",
                "type": 0
            }
        },
        "variableMap": {
            "writeable": True,
            "DATA_STATE_CHECK_EDIT": ""
        },
        "measureMap": {},
        "sumScheme": None,
        "decimal": ""
    },
    "filterInfo": {
        "filterFormula": []
    },
    "regionKey": "36b09a41-5cd5-4e54-aa50-c2337e9e35c2",
    "pagerInfo": {
        "offset": 0,
        "limit": 500
    },
    "restructureInfo": {},
    "contextEntityId": "",
    "contextFilterExpression": ""
}

def get_data(payload,regionKey,quyuma):
  payload['regionKey'] = regionKey 
  payload['context']['dimensionSet']['MD_ORG']['value'] = quyuma
  headers['Content-Length'] = str(len(json.dumps(payload)))
  response = requests.post(url, headers=headers, data=json.dumps(payload))
  if response.status_code == 200:
    data = response.json()
    # print("获取的数据:", data['data'])
    return data['data']
  else:
    print("请求失败，状态码:", response.status_code)

data1 = []
data2 = []
qym = ['330700', '330702', '330703', '330723', '330726', '330727', '330781', '330782', '330783', '330784']

for area_code in qym:
    result = {}
    result['zdyh'] = get_data(payload, '36b09a41-5cd5-4e54-aa50-c2337e9e35c2', area_code)
    if result['zdyh'] is not None:
        data1.append(result['zdyh'])
    result['ybyh'] = get_data(payload, '72ee125c-9d2b-4d9d-a752-2ef301194f7b', area_code)
    if result['ybyh'] is not None:
        data2.append(result['ybyh'])


# print("重大隐患:",data1)
# print("一般隐患:",data2)
# exit()

import pandas as pd

# 定义表头
columns = ['ID', '序号', '项目代码', '项目名称', '统一社会信用代码', '企业名称', '责任人', '标段', '问题责任人', '所属范围', '隐患类型码', '隐患类型', '隐患描述', '整改要求', '整改截止日期', '整改期限', '是否整改', '自查排查', '备注1', '备注2', '备注3']

# 处理重大隐患数据
flat_data1 = []
for sublist in data1:
    if sublist:  # 只处理非空列表
        flat_data1.extend(sublist)
df = pd.DataFrame.from_records(flat_data1, columns=columns)
df.to_excel('./Pyjd/重大隐患排查系统/重大隐患.xlsx', index=False)

# 修改一般隐患的列定义
columns2 = ['ID', '序号', '项目代码', '项目名称', '所属范围', '标段', '隐患描述', '整改截止日期', '整改期限', '是否整改', '自查排查', '备注1', '备注2', '备注3']

# 处理数据时需要先将嵌套列表展平
flat_data2 = []
for sublist in data2:
    if sublist:  # 只处理非空列表
        flat_data2.extend(sublist)

# 使用展平后的数据创建DataFrame
df2 = pd.DataFrame.from_records(flat_data2, columns=columns2)
df2.to_excel('./Pyjd/重大隐患排查系统/一般隐患.xlsx', index=False)


